﻿using System.Configuration;

namespace RESTController
{
    public class RESTControllerSettings : ConfigurationSection
    {
        private const string ALLOW_JSON_GET = "allowJsonGet";
        private const string DELETE_HTTP_METHOD = "deleteHttpMethod";
        private const string EDIT_HTTP_METHOD = "editHttpMethod";
        private const string NEW_HTTP_METHOD = "newHttpMethod";
        private const string REDIRECT_TO_INDEX = "redirectToIndex";
        private const string PAGE_SIZE = "pageSize";
        private const string USE_PAGING = "usePaging";

        private static readonly object settings = ConfigurationManager.GetSection("RESTControllerSettings");

        public static RESTControllerSettings Settings
        {
            get { return (settings == null) ? new RESTControllerSettings() : (RESTControllerSettings)settings; }
        }

        public override bool IsReadOnly()
        {
            return false;
        }

        [ConfigurationProperty(ALLOW_JSON_GET, DefaultValue = true, IsRequired = false)]
        public bool AllowJsonGet
        {
            get { return (bool)this[ALLOW_JSON_GET]; }
            set { this[ALLOW_JSON_GET] = value; }
        }

        /// <summary>
        /// Gets and sets the HTTP method used for delete operations. By default, this is a DELETE method, but you may choose to use POST (or even GET if you're really daring).
        /// </summary>
        [ConfigurationProperty(DELETE_HTTP_METHOD, DefaultValue = "DELETE", IsRequired = false)]        
        public string DeleteHttpMethod
        {
            get { return (string)this[DELETE_HTTP_METHOD]; }
            set { this[DELETE_HTTP_METHOD] = value; }
        }

        /// <summary>
        /// Gets and sets the HTTP method used for edit operations. By default, this is a POST operation.
        /// </summary>
        [ConfigurationProperty(EDIT_HTTP_METHOD, DefaultValue = "POST", IsRequired = false)]
        public string EditHttpMethod
        {
            get { return (string)this[EDIT_HTTP_METHOD]; }
            set { this[EDIT_HTTP_METHOD] = value; }
        }

        /// <summary>
        /// Gets and sets the HTTP method used for new operations. By default, tis is a POST operation.
        /// </summary>
        [ConfigurationProperty(NEW_HTTP_METHOD, DefaultValue = "POST", IsRequired = false)]
        public string NewHttpMethod
        {
            get { return (string)this[NEW_HTTP_METHOD]; }
            set { this[NEW_HTTP_METHOD] = value; }
        }

        /// <summary>
        /// If true, successful edits and adds will redirect to the "Index" page.
        /// If false, edits and adds will redirect to the "Show" page.
        /// </summary>
        [ConfigurationProperty(REDIRECT_TO_INDEX, DefaultValue = false, IsRequired = false)]
        public bool RedirectToIndex
        {
            get { return (bool)this[REDIRECT_TO_INDEX]; }
            set { this[REDIRECT_TO_INDEX] = value; }
        }

        [ConfigurationProperty(PAGE_SIZE, DefaultValue = 50, IsRequired = false)]
        public int PageSize
        {
            get { return (int)this["pageSize"]; }
            set { this[PAGE_SIZE] = value; }
        }

        /// <summary>
        /// Should the application use paging features on index or list views?
        /// </summary>
        [ConfigurationProperty(USE_PAGING, DefaultValue = false, IsRequired = false)]
        public bool UsePaging
        {
            get { return (bool)this[USE_PAGING]; }
            set { this[USE_PAGING] = value; }
        }
    }
}
